﻿#----------------------------------------------------------------------------
# Purpose: To demonstrate programming in natural language.
# Version: Thinknowlogy 2018r4 (New Science)
#----------------------------------------------------------------------------
# Copyright (C) 2009-2018, Menno Mafait. Your suggestions, modifications,
# corrections and bug reports are welcome at http://mafait.org/contact/
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# A summary of the "tower of Hanoi" puzzle:
#----------------------------------------------------------------------------
# The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower,
# and sometimes pluralised) is a mathematical game or puzzle. It consists
# of three rods, and a number of disks of different sizes which can slide
# onto any rod. The puzzle starts with the disks in a neat stack in
# ascending order of size on one rod, the smallest at the top, thus
# making a conical shape.
#
# The objective of the puzzle is to move the entire stack to another rod
# obeying the following rules:
#
# - Only one disk may be moved at a time.
# - Each move consists of taking the upper disk from one of the rods and
#   sliding it onto another rod, on top of the other disks that may
#   already be present on that rod.
# - No disk may be placed on top of a smaller disk.
#----------------------------------------------------------------------------
# See for the full text: http://en.wikipedia.org/wiki/Tower_of_Hanoi
#----------------------------------------------------------------------------

# Define the moves.
A move is between-start-and-swap, between-start-and-goal, between-swap-and-goal, done-between-start-and-swap, done-between-start-and-goal or done-between-swap-and-goal.

# Display the moves.
If the move is done-between-start-and-swap or the move is done-between-start-and-goal or the move is done-between-swap-and-goal then display "* start pole:\t~+'start pole'<a>`~.\n* swap pole:\t~+'swap pole'<a>`~.\n* goal pole:\t~+'goal pole'<a>`~.\n\n".

# The rules for moving the discs.
If the move is between-swap-and-goal then if the swap pole is assigned and the goal pole is not assigned or the swap pole is assigned and the head of the swap pole is smaller than the head of the goal pole then move the head of the swap pole to the goal pole, the move is done-between-swap-and-goal and display "Moved a disc from swap to goal:\n" else move the head of the goal pole to the swap pole, the move is done-between-swap-and-goal and display "Moved a disc from goal to swap:\n".
If the move is between-start-and-goal then if the start pole is assigned and the goal pole is not assigned or the start pole is assigned and the head of the start pole is smaller than the head of the goal pole then move the head of the start pole to the goal pole, the move is done-between-start-and-goal and display "Moved a disc from start to goal:\n" else move the head of the goal pole to the start pole, the move is done-between-start-and-goal and display "Moved a disc from goal to start:\n".
If the move is between-start-and-swap then if the start pole is assigned and the swap pole is not assigned or the start pole is assigned and the head of the start pole is smaller than the head of the swap pole then move the head of the start pole to the swap pole, the move is done-between-start-and-swap and display "Moved a disc from start to swap:\n" else move the head of the swap pole to the start pole, the move is done-between-start-and-swap and display "Moved a disc from swap to start:\n".

# Before each new move, the order is set in which the rules must be executed.
If the execution order is odd-first then if the move is not assigned or the move is done-between-swap-and-goal then the move is between-start-and-goal else if the move is done-between-start-and-goal then the move is between-start-and-swap else the move is between-swap-and-goal.
If the execution order is even-first then if the move is not assigned or the move is done-between-swap-and-goal then the move is between-start-and-swap else if the move is done-between-start-and-swap then the move is between-start-and-goal else the move is between-swap-and-goal.

# At the start, determine the order in which the rules must be executed.
If the start pole is assigned, the swap pole is not assigned and the goal pole is not assigned then if the number of the start pole is odd then the execution order is odd-first and display "\nThe number of discs is odd. So, the rule order is: Move first from start to goal, then from start to swap.\n\n" else the execution order is even-first and display "\nThe number of discs is even. So, the rule order is: Move first from start to swap, then from start to goal.\n\n".

# Define the end.
If the move is assigned, the start pole is not assigned, the swap pole is not assigned and the goal pole is assigned then the move is empty, the execution order is empty and display "Done.\nTo start the puzzle again, click button «Clear your mind.» and then «Read the file \"English/programming/tower of Hanoi\".»".

# Now add an odd or even number of discs in reverse order,
# the biggest first (e.g. D or E), and the smallest (A) on top, like:
# - For an even number of discs:  Add D, C, B and A to the start pole.
# - For an odd number of discs:   Add E, D, C, B and A to the start pole.